package com.jjwxc.jwjskandriod.widget.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

import com.jjwxc.jwjskandriod.R;

public abstract class BaseBottomDialogFragment extends BaseDialogFragment {
    public Dialog dialog;
    private int gravity;
    private int animation = -1;
    private int alpha = -1; // [0, 1]

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        dialog = new Dialog(getActivity(), R.style.BottomDialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // 設置Content前設定
        dialog.setCanceledOnTouchOutside(true); // 外部點擊取消
        dialog.setContentView(setContentViewID());
        // 設置寬度為屏寬, 靠近屏幕底部。
        Window window = dialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();

        // 设置出现位置
        if (gravity == Gravity.TOP) {
            lp.gravity = Gravity.TOP;
        } else if (gravity == Gravity.CENTER) {
            lp.gravity = Gravity.CENTER;
        } else {
            lp.gravity = Gravity.BOTTOM; // 緊貼底部
        }
        // 设置暗度
        if (alpha >=0 && alpha <= 1) {
            lp.alpha = alpha;
        }

        lp.width = WindowManager.LayoutParams.MATCH_PARENT; // 寬度持平
        window.setAttributes(lp);

        if (animation != -1) {
            window.setWindowAnimations(animation);
        }

        findViewIDToOnClick();
        return dialog;
    }

    public abstract int setContentViewID();

    public abstract void findViewIDToOnClick();

    public Dialog getBaseDialog() {
        if (dialog != null) {
            return dialog;
        }
        return null;
    }

    public void setGravity(int gravity) {
        this.gravity = gravity;
    }

    public void setAlpha(int alpha) {
        this.alpha = alpha;
    }

    public void setAnimationStyle(int animation) {
        this.animation = animation;
    }
}